#pragma once

#include <iostream>
#include <string>
#include <memory>
#include <sys/types.h>
#include <sys/socket.h>
#include <functional>
#include "Connection.hpp"
#include "Common.hpp"
#include "Log.hpp"
#include "InetAddr.hpp"

using namespace LogModule;

#define SIZE 1024

// 普通的fd封装

class Channel : public Connection
{
public:
    Channel(int sockfd, const InetAddr &client) : _sockfd(sockfd), _client_addr(client)
    {
        SetNonBlock(sockfd);
        // SetFd(sockfd);
    }
    void RegisterHandler(handler_t handler)
    {
        _handler = handler;
    }
    void Recver() override
    {
        // LOG(LogLevel::DEBUG) << "时间被派发到Channel模块";
        char buffer[SIZE];
        while (true)
        {
            buffer[0] = 0;
            ssize_t n = recv(_sockfd, buffer, sizeof(buffer) - 1, 0);
            if (n > 0)
            {
                buffer[n] = 0;
                _inbuffer += buffer;
            }
            else if (n == 0)
            {
                Excepter();
                return;
            }
            else
            {
                if (errno == EAGAIN || errno == EWOULDBLOCK)
                {
                    break;
                }
                else if (errno == EINTR)
                {
                    continue;
                }
                else
                {
                    Excepter();
                    return;
                }
            }
        }
        if (!_inbuffer.empty())
        {
            _outbuffer += _handler(_inbuffer);
        }
        if (!_outbuffer.empty())
        {
            GetOwner()->EnableReadWrite(_sockfd,true,true);
            //Sender();
        }
    }
    void Sender() override
    {
        while (true)
        {
            ssize_t n = send(_sockfd, _outbuffer.c_str(), _outbuffer.size(), 0);
            if (n > 0)
            {
                _outbuffer.erase(0, n);
                if (!_outbuffer.empty())
                    break;
            }
            else if (n == 0)
            {
                break;
            }
            else
            {
                if (errno == EAGAIN || errno == EWOULDBLOCK)
                    break;
                if (errno == EINTR)
                    continue;
                else
                {
                    Excepter();
                    return;
                }
            }
        }

        if(!_outbuffer.empty())
        {
            GetOwner()->EnableReadWrite(_sockfd,true,true);
        }
        else
        {
            GetOwner()->EnableReadWrite(_sockfd,true,true);
        }
    }
    void Excepter() override
    {
    }
    int GetFd() override
    {
        return _sockfd;
    }
    std::string Inbuffer(std::string *out)
    {
        return _inbuffer;
    }
    void AppendOutBuffer(std::string &out)
    {
        _outbuffer += out;
    }
    ~Channel()
    {
    }

private:
    int _sockfd;
    std::string _inbuffer;
    std::string _outbuffer;

    // 客户端信息
    InetAddr _client_addr;

    handler_t _handler;
};